from fastapi import FastAPI
import uvicorn
from tortoise.contrib.fastapi import register_tortoise
from fastapi.middleware.cors import CORSMiddleware

# 数据库配置文件
from app.settings.settings import TORTOISE_ORM

# 导入路由
from app.api.user import user
from app.api.gushi import poem
from app.api.ai import ai
from app.api.work import work

# 注册fastapi
app = FastAPI()

# 数据库配置和注册
# fastAPI一旦运行，这个就开始运行了
register_tortoise(
    app=app,
    config=TORTOISE_ORM,
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # *表示允许所有来源
    allow_credentials=True,
    allow_methods=["GET", "POST", "PUT", "DELETE"],
    allow_headers=["*"],
)


@app.get("/")
async def root():
    return {"message": "Hello World"}

# 注册路由
app.include_router(user, prefix="/user", tags=["user"])
app.include_router(poem, prefix="/gushi", tags=["古诗"])
app.include_router(ai, prefix="/ai", tags=["AI"])
app.include_router(work, prefix="/work", tags=["作品"])

if __name__ == "__main__":
    uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)
